﻿namespace ImageProcessing.ImageProcessor
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// class IMG_Compression
    /// </summary>
    public static class IMG_Compression
    {
        /// <summary>
        /// IMs the g_ compress.
        /// </summary>
        /// <param name="img_OldBuffer">The img_ old buffer.</param>
        /// <param name="img_Bpp">The img_ BPP.</param>
        /// <returns>the new buffer with bpp as required</returns>
        public static Entities.IMG_Image IMG_Compress(Entities.IMG_Image img_OldBuffer, float img_Bpp)
        {
            try
            {
                Entities.Pixel img_NewPixel;

                byte myByte = new byte();
                myByte = (byte)(256 - Math.Pow(byte.Parse("2"), byte.Parse("8") - (byte)img_Bpp));
                int img_totalLength = img_OldBuffer.Height;
                int img_totalWidth = img_OldBuffer.Width;

                Entities.IMG_Image img_ResultBuffer = new Entities.IMG_Image(img_totalWidth, img_totalLength);
                int index = img_totalWidth * img_totalLength;
                for (int i = 0; i < index; i++)
                {
                    img_NewPixel = new Entities.Pixel(
                        (int)((byte)img_OldBuffer.ImagePixels[i].Red & myByte),
                        (int)((byte)img_OldBuffer.ImagePixels[i].Green & myByte),
                        (int)((byte)img_OldBuffer.ImagePixels[i].Blue & myByte));

                    img_ResultBuffer.ImagePixels[i] = img_NewPixel;
                }

                return img_ResultBuffer;
            }
            catch (Exception img_ex)
            {
                Logger.LogException(img_ex);
                throw;
            }
        }
    }
}
